<?php

namespace Application\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Application\Model\Personnel;
use Zend\Log\Logger;
use Zend\Log\Writer\Stream;


use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

class AgendaTable extends AbstractTableGateway {

	protected $table ='agenda';
	protected $tablename ='agenda';
	var  $logger = null;
	var  $writer = null;
	
	var $dossier ='../lamisa.burundi/public/img/avatar/';
	
		
	public function __construct(Adapter $adapter) {
		$this->adapter = $adapter;
		$this->resultSetPrototype = new ResultSet(new Personnel());
		$this->initialize();
		
		
	}

	
	public function fetchAll()
	{
		$resultSet = $this->select();
		return $resultSet;
	}
	
	
	/*public function fetchAll($paginated=false)
	{
		if($paginated) {
			// create a new Select object for the table album
			$select = new Select('agenda');
			// create a new result set based on the Album entity
			$resultSetPrototype = new ResultSet();
			$resultSetPrototype->setArrayObjectPrototype(new Agenda());
			// create a new pagination adapter object
			$paginatorAdapter = new DbSelect(
					// our configured select object
					$select,
					// the adapter to run it against
					$this->tableGateway->getAdapter(),
					// the result set to hydrate
					$resultSetPrototype
			);
			$paginator = new Paginator($paginatorAdapter);
			return $paginator;
		}
		$resultSet = $this->tableGateway->select();
		return $resultSet;
	}
	*/

	public function getTache($id)
	{
		$logger->info('Methode getTache');
		
		$id = (int) $id;
		$rowset = $this->select(array('id_tache' => $id
								));
		$row = $rowset->current();
		if (!$row) {
			//throw new \Exception("Could not find row $id");
			$row = "Error";
		}
		return $row;
	}

	public function getTacheByUserId($id)
	{
		$stream = @fopen('fichiers/logs/'.date('Y-m-d').'_log.log', 'a', false);
		$writer = new Stream($stream);
		$logger = new Logger();
		$logger->addWriter($writer);
		$logger->info('Methode getTacheByUserId');
		$id = (int) $id;
		$rowset = $this->select(array('id_user' => $id,
		 'flag_isdone'=>0));
		//$row = $rowset->current();
		if (!$rowset) {
			//throw new \Exception("Could not find row $id");
			$rowset = "Error";
		}
		return $rowset;
	}
	
	
	
	
	public function addAgenda($type_tache,$id_demande,$id_user, $titre,$description,$date_crea,$date_limite, $auteur, $flag_urgent,$flag_isdone,$date_traitement)
	{
		
		$data = array(
				'type_tache' => $type_tache,
				'id_demande'	=>$id_demande,
				'id_user' => $id_user,
				'titre' => $titre,
				'description' => $description,
				'date_crea' => $date_crea,
				'date_limite' => $date_limite,
				'auteur' => $auteur,
				'flag_urgent' => $flag_urgent,
				'flag_isdone' => $flag_isdone,
				'date_traitement' => $date_traitement,
		);
		$this->insert($data);
	}
	
	
/*	
	public function savePersonnel(Personnel $user)
	{
		$data = array(

				'username' => $user->username,
				'id_personnel' => $user->id_personnel,
				'password' => $user->password,
				'email' => $user->email,
				'avatar' => $user->avatar,
				'isadmin' => $user->isadmin,
				'isblocked' => $user->isblocked,
				'signup_date' => $user->signup_date,
		);
		$id = (int)$user->id_user;
		if ($id == 0) {
			$this->insert($data);
		} else {
			if ($this->getUser($id)) {
				$this->update($data, array('id_user' => $id));
			} else {
				throw new \Exception('Form id does not exist');
			}
		}
	}
*/
	
	//TODO
	/*public function addFile($id_user,$nom_fichier,$url,$type_fichier,$date_upload)
	{
		$data = array(
				//'id_file' => $id_file,
				'id_user' => $id_user,
				'nom_fichier' => $nom_fichier,
				'url' => $url,
				'type_fichier' => $type_fichier,
				'date_upload' => $date_upload,			
		);
		$this->insert($data);
	}
*/
	public function updateAgenda($reponse,$id_tache)
	{
		$stream = @fopen('fichiers/logs/'.date('Y-m-d').'_log.log', 'a', false);
		$writer = new Stream($stream);
		$logger = new Logger();
		$logger->addWriter($writer);
		$logger->info('Methode updateAgenda reponse: '.$reponse );
		$logger->info('Methode updateAgenda id_tache: '.$id_tache );
		
		$date =date("Y-m-d");
		$data = array(
				//'id_user' => $id_user,
				'flag_isdone' => 1,
				'reponse' => $reponse,
				'date_traitement' => $date,
		);
		$this->update($data, array('id_tache' => $id_tache));
		//return 1;
	}
	/*public function deleteAlbum($id)
	{
		$this->delete(array('id_user' => $id));
	}
	
	*/
}

?>
